import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

from sklearn.datasets import load_breast_cancer

# data sets
data = load_breast_cancer()
print(data)

x = data.data
y = data.target

m, n = x.shape

df_x = pd.DataFrame(x)
df1 = df_x.corr()
sns.heatmap(df1, annot=None)
plt.show()

# 创建数据
data = np.random.randn(m, n)  # 数据
df = pd.DataFrame(data)
df2 = df.corr()
print(df.corr())
sns.heatmap(df2, annot=None)
plt.show()


# feature 0_means
xmean = np.mean(x, axis=0)
x = x - xmean

plt.scatter(x[:, 0], x[:, 2], c=y)
plt.show()

cov_mat = np.dot(x.T, x) / m
print("C: \n", cov_mat[:4, :4])

U, S, V = np.linalg.svd(cov_mat)
# print("S: ", S, S[0], np.sum(S[:0]), np.sum(S[:1]))
print("S: ", S)
k = 2
P = V[:k, :].T
z = np.dot(x, P)

plt.plot(S)
plt.grid()
plt.show()

k_pca = []
Stotal = np.sum(S)
for i in range(len(S)):
    k_S = np.sum(S[:(i + 1)]) / Stotal
    k_pca.append(k_S)

plt.plot(k_pca)
plt.grid()
plt.show()

cov_z = np.dot(z.T, z) / m
print("cov_z: \n", cov_z)

print(z[:4])

# data visualization
plt.scatter(z[:, 0], z[:, 1], c=y)
plt.show()

from sklearn.decomposition import PCA
model = PCA(n_components=2)
z = model.fit_transform(x)
print('----------------')
print(z[:4])

# ==========
U1, S1, V1 = np.linalg.svd(x)
P = V1[:k, :].T
z1 = np.dot(x, P)
print(z1[:4])

z11 = np.dot(U1[:, :k], np.diag(S1[:k]))
print(z11[:4])
